package com.example.usercenter;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.usercenter.utils.CaptchaUtil;
import com.example.usercenter.utils.RegisterUtil;

import java.util.concurrent.atomic.AtomicInteger;

public class RegsiterActivity extends AppCompatActivity implements View.OnClickListener {

    EditText et_regsiter_phoneNumber;
    EditText et_regsiter_captcha;
    EditText et_regsiter_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regsiter);

        Button bt_getCaptcha = findViewById(R.id.bt_getCaptcha);
        Button bt_registerinregister = findViewById(R.id.bt_registerinregister);

        et_regsiter_phoneNumber = findViewById(R.id.et_regsiter_phoneNumber);
        et_regsiter_captcha = findViewById(R.id.et_regsiter_captcha);
        et_regsiter_password = findViewById(R.id.et_regsiter_password);

        bt_getCaptcha.setOnClickListener(this);
        bt_registerinregister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        AtomicInteger captcha = new AtomicInteger();
        switch (v.getId()){
            case R.id.bt_getCaptcha:
                new Thread(()->{
                    try {

                        if(et_regsiter_phoneNumber.getText().toString()==null |
                                "".equals(et_regsiter_phoneNumber.getText().toString())){
                            Toast.makeText(RegsiterActivity.this,
                                    "手机号不能为空！"
                                    ,Toast.LENGTH_SHORT).show();
                        }else{
                            captcha.set(CaptchaUtil.getCaptcha(et_regsiter_phoneNumber.getText().toString()));

                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(()->{
                        et_regsiter_captcha.setText(captcha.toString());
                    });
                }).start();
                break;

            case R.id.bt_registerinregister:
                AtomicInteger registerCode = new AtomicInteger();
                new Thread(()->{
                    try {
                        registerCode.set(RegisterUtil.register(et_regsiter_phoneNumber.getText().toString(),
                                et_regsiter_password.getText().toString(),
                                et_regsiter_captcha.getText().toString()));

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(()->{
                        //如果注册成功，跳转到登录页面
                        System.out.println("registerCode:"+registerCode);
                        if(registerCode.get()==200){
                            System.out.println("是否进入if:");
                            Toast.makeText(RegsiterActivity.this,
                                    "恭喜您注册成功，您的手机号是"+et_regsiter_phoneNumber.getText().toString()+"!"
                                        ,Toast.LENGTH_SHORT).show();

                            startActivity(new Intent(RegsiterActivity.this,MainActivity.class));
                        }else{
                            Toast.makeText(RegsiterActivity.this,
                                    "注册失败，该手机号已注册或请检查各输入框是否完整",Toast.LENGTH_SHORT).show();
                        }
                    });
                }).start();
                break;


        }
    }
}